<template>
    <div style="width: 100%;height: 100%">
        <component ref="treeComponent"
                   :is="currentView"
                   :html="html"
                   :curApp="curApp"
                   :appKey="appKey"
                   :colsJsonStr="colsJsonStr"
                   :searchJsonStr="searchJsonStr"
                   :topBtnsJsonStr="topBtnsJsonStr"
                   :buttons="buttons"
                   :allButtons="allButtons"
                   :dialogBox="dialogBox"
        >
        </component>
    </div>
</template>

<script>
import FormCustomPublic from "../FormCustomPublic";
import BusEvent from "../BusEvent";
import {RxEmpty,FormBoListPreviewList} from '@lowcode/jpaas-share-component';
import {mapState} from "pinia";
import useMain from "@/stores/useMain.js";
import DialogBox from "@/assets/js/dialogBox.js";
import FormBoListApi from "@/common/api/form/formBoList.js"
export default {
    name: "TreeView",
    inject:['appId'],
    components: {
        RxEmpty
    },
    mixins:[FormCustomPublic,BusEvent],
    computed: {
        ...mapState(useMain, ["buttons", "allButtons", "appKey", "curApp"]),
    },
    data() {
        return {
            currentView:RxEmpty,
            html:"",
            params:"",
            listAlias:"",
            colsJsonStr:"",
            topBtnsJsonStr:"",
            searchJsonStr:"",
            dialogBox: DialogBox,
            formdata:{},
            user:{},
        }
    },
    created() {
        this.init();
    },
    methods: {
        getTable(){
          return this.$refs.treeComponent.$refs.current.$refs.rxTree.rxTreeVue;
        },
        getData() {
          return this.getTable().curTreeData;
        },
        getSelectedRows(){
          return this.getTable().checkeds;
        },
        init(){
            if(this.config){
                //取路由参数
                let params=this.getRouterParams();
                let paramStr="";
                //读取配置的参数
                for (let i = 0; i < this.config.receive.mapping.length; i++) {
                    let o = this.config.receive.mapping[i];
                    if(o.valueSource == 'param'){
                        if(this.config.receive.type== "url"){
                            if (params[o.valueDef]) {
                                paramStr += o.name + '=' + params[o.valueDef] + '&';
                            }
                        }else {
                            if (params[o.name]) {
                                paramStr += o.name + '=' + params[o.name] + '&';
                            }
                        }
                    }
                }
                let self = this;
                this.getParams(this.config.receive.mapping,function (params) {
                    for (let key in params) {
                        paramStr += key + '=' + params[key] + '&';
                    }
                    self.params=paramStr;
                    self.listAlias=self.config.alias;
                    self.initTree();
                });
            }
        },
        async initTree(){
            if (!this.listAlias) {
                return;
            }
            let self = this;
            let parameter = {key: this.listAlias,  params: this.params};
            let res = await FormBoListApi.dialog(parameter,this.appId);
            if (!res.success) {
                return;
            }
            let record = res.data;
            self.html = record.listHtml;
            self.colsJsonStr = record.colsJson;
            self.searchJsonStr = record.searchJson;
            self.topBtnsJsonStr = record.topBtnsJson;
            this.currentView =FormBoListPreviewList;
        },
        /**
         * 处理页面定制公共事件
         * @param args
         */
        handPublicReceive(args){
            let self_=this;
            //通过自定义的事件
            if(args && args.type=="custom" && args.params){
                if(self_.$refs.treeComponent){
                    self_.$refs.treeComponent.$refs.current.loadByParams(args.params);
                }
                return;
            }
            let receive = this.config.receive;
            let inputParams = args.params;
            let params = {};
            //读取配置的参数
            for (let i = 0; i < receive.mapping.length; i++) {
                let o = receive.mapping[i];
                //处理没有配置值来源的情况。
                if(inputParams[o.valueDef]){
                    params[o.name]=inputParams[o.valueDef];
                }
                else {
                    params[o.name]="";
                }
            }
            this.getParams(receive.mapping,function (data) {
                if(data){
                    for (let key in data) {
                        params[key] = data[key];
                    }
                }
                if(self_.$refs.treeComponent){
                    self_.$refs.treeComponent.$refs.current.loadByParams(params);
                }
            })
        },
    },
    watch: {},
}
</script>

<style scoped>

</style>
